using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_FacilityManagement : admin_BaseUserControl
{
     FacilityDAO daoFac = new FacilityDAO();
        Facility fac = new Facility();
    #region properties
    public string ImageURL
    {
        get { return fac.ImageURL; }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        ActiveAccording = "facility";
        if (Request["mode"] == "1")
        {
            lnkAdd.Visible = false;
            lnkDelete.Visible = true;
            lnkUpdate.Visible = true;
            try
            {
                int id = int.Parse(Request["id"]);
                CurrentID = Request["id"];
                fac = daoFac.GetFacility(id);
                txtName.Text = fac.Name;
                txtDescription.Text = fac.Description;
                txtPrice.Text = fac.Price.ToString();
                txtDiscount.Text = fac.Discount.ToString();
                chkState.Checked = fac.State;
                FCKeditor1.Value = fac.Article;
                int index = 0;
                foreach (ListItem item in cmbCategory.Items)
                {
                    if (item.Value == fac.Type.Id.ToString())
                    {
                        cmbCategory.SelectedIndex = index;
                        break;
                    }
                    index++;
                }
            }
            catch (Exception ex)
            {
                ShowMessage("Error");
            }
        }
        else
        {
            txtName.Text = "";
            txtDescription.Text = "";
            txtDiscount.Text = "0";
            txtPrice.Text = "0";
            
                lnkAdd.Visible = true;
                lnkDelete.Visible = false;
                lnkUpdate.Visible = false;
      
        }
    }
    public void LoadData()
    {
        FacilityTypeDAO daoFacType = new FacilityTypeDAO();
        cmbCategory.DataSource = daoFacType.GetAllFacilityTypes("%");
        cmbCategory.DataTextField = "Name";
        cmbCategory.DataValueField = "Id";
        cmbCategory.DataBind();
    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        int id = int.Parse(CurrentID);
        fac.Id = id;
        fac.Name = txtName.Text;
        fac.Description = txtDescription.Text;
        fac.Type.Id = int.Parse(cmbCategory.SelectedValue);
        fac.Article = FCKeditor1.Value;
        fac.ImageURL = Request["txtImage"];
        fac.Price = decimal.Parse(txtPrice.Text);
        fac.Discount = decimal.Parse(txtDiscount.Text);
        fac.State = chkState.Checked;
        fac.ModifiedDate = DateTime.Now;
        fac.ModifiedBy = 1;
        if (daoFac.Update(fac) > 0)
        {
            ShowMessage("Information is updated");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
        }
        else ShowMessage("Error");
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        fac = new Facility();
        fac.Name = txtName.Text;
        fac.Description = txtDescription.Text;
        fac.Type.Id = int.Parse(cmbCategory.SelectedValue);
        fac.Article = FCKeditor1.Value;
        fac.ImageURL = Request["txtImage"];
        fac.Price = decimal.Parse(txtPrice.Text);
        fac.Discount = decimal.Parse(txtDiscount.Text);
        fac.State = chkState.Checked;
        fac.ModifiedDate = DateTime.Now;
        fac.ModifiedBy = 1;
        if (daoFac.Insert(fac) > 0)
        {
            ShowMessage("New facility information is created");
            CurrentID = fac.Id.ToString();
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
        }
        else ShowMessage("Error");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(CurrentID);
        if (daoFac.Delete(id) > 0)
        {            
            Response.Redirect("Default.aspx?ctrl=fac_list");
        }
        else ShowMessage("Error occured when trying to deleted facility information");
    }
   
}
